package com.enjoy;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.util.List;

public class Main {
    public static void main(String[] args) {

        UnpooledDataSource dataSource = new UnpooledDataSource();

        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
        dataSource.setDriver("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        // 创建 Configuration 对象
        Configuration configuration = new MybatisConfiguration(environment);
        // 加入一个映射器
        configuration.addMappers("com.enjoy");

        SqlSession sqlSession = new MybatisSqlSessionFactoryBuilder().build(configuration).openSession();



        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

        Employee employee = new Employee();
        employee.setAge(1);
        employee.setLastName("aa");
        employee.setEmail("bb");
        employee.setGender(1);
        mapper.insert(employee);

        // 提交事务
        sqlSession.commit();


        System.out.println("插入成功");

        List<Employee> employees = mapper.selectByMap(null);

        System.out.println(employees);

        sqlSession.close();
    }
}
